package com.sloth.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.sloth.service.AuthenticationService;
import com.sloth.util.JsonUtil;
//import net.sf.json.JSONObject;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AuthenticationController {
    @Autowired
    private AuthenticationService authenticationService;


    @GetMapping("/peron/personName/{personName}/personId/{personId}")
    @ResponseBody
    public JsonNode personCard(@PathVariable("personName") String personName, @PathVariable("personId") String personId) throws IOException {

        Map<String, String> params = new HashMap<>();
        params.put("idcard", personId);
        params.put("name", personName);
        System.out.println("personId: " + personId);
        System.out.println("personName: " + personName);

        String str = " {\"code\":\"0\",\"message\":\"成功\",\"result\":{\"name\":\"王震\",\"idcard\":\"230223199909270918\",\"res\":\"1\",\"description\":\"一致\",\"sex\":\"男\",\"birthday\":\"19990927\",\"address\":\"黑龙江省齐齐哈尔市依安县\"}}";
        JsonNode result = JsonUtil.stringToJson(str);

//        JsonNode person = JsonUtil.stringToJson(Authentication.postForm(params));
        return result;
    }

    @PostMapping("personCardImg")
    @ResponseBody
    public JsonNode personCardImg(@RequestParam("myimage") MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html");

//            Object result = authenticationService.getPersonCard(multipartFile);
        try {
            System.out.println("位置1");

//            String str = " {\"code\":\"0\",\"message\":\"成功\",\"result\":{\"name\":\"王震\",\"idcard\":\"230223199909270918\",\"res\":\"1\",\"description\":\"一致\",\"sex\":\"男\",\"birthday\":\"19990927\",\"address\":\"黑龙江省齐齐哈尔市依安县\"}}";
//            JsonNode person = JsonUtil.stringToJson(str);

            JsonNode person = authenticationService.getPersonCard(multipartFile);
            return person;
        } catch (Exception e) {
            System.out.println("错误位置1");
            e.printStackTrace();
            return JsonUtil.stringToJson(ServerResponse.getFail(null).toString());
        }
    }
}
